import CURDHelper from '@/utils/CURDHelper'
import PageConfig from '@/utils/pageConfig'
import { getBtnCtr } from '@/utils/BtnControl'
import Query from '@/components/Query';
import { isNumber } from '@/utils/validate';
import { Tooltip, VxeUI } from 'vxe-pc-ui';
import XEUtils from 'xe-utils';
export default {
  name: 'CustomerSystems',
  components: {
    Query
  },
  data() {
    const gltxItemRender = {
      name: 'VxeSelect',
      options: [],
      events: {
        change: (e) => {
          this.GltxOptions.map((item) => {
            console.log(item)
            if (item.id == e.row.txid) {
              e.row.txgg = item.txgg;
              e.row.rzbz = item.rzbz;
            }
          })
        }
      }
    }
    return {
      gltxItemRender,
      componentSize: PageConfig.ComponentSize,
      duration: PageConfig.Duration,
      btnPromission: '',
      editModal: false,
      QueryConditions: [],
      BaseApiUrl: '/api/Xmjckhxx',
      BaseChildApiUrl: '/api/Xmkhgltx',
      editRow: undefined,
      CalendarView: false,
      QueryConfig: [{
        title: '客户名称',
        field: 'khmc',
        type: 'input',
        value: '',
        placeholder: '请输入客户名称以查询',
      }, {
        title: '体系名称',
        field: 'khmc',
        type: 'input',
        value: '',
        placeholder: '请输入体系名称以查询',
      }],
      GridOptions: {
        id:'CustomerSystemsMainGrid',
        border: true,
        stripe: true,
        loading: false,
        size: PageConfig.ComponentSize,
        height: 350,
        columnConfig: {
          resizable: true
        },
        rowConfig: {
          isHover: true
        },
        checkboxConfig: {
          highlight: true,
          range: true
        },
        exportConfig: {
        },
        toolbarConfig: {
          custom: true
        },
        customConfig:{
          mode:'drawer',
          storage:true
        },
        columns: [
          { type: 'seq', width: 50 },
          { type: 'checkbox', width: 35 },
          { field: 'khbm', title: '客户编码', sortable: true, minWidth: 100, showOverflow: true },
          { field: 'khmc', title: '客户名称', sortable: true, minWidth: 150, showOverflow: true },
          { field: 'khjc', title: '客户简称', sortable: true, minWidth: 100, showOverflow: true },
          {
            field: 'kyrq', title: '开业日期', sortable: true, minWidth: 120, showOverflow: true,
            formatter({ cellValue }) {
              return XEUtils.toDateString(cellValue, 'yyyy-MM-dd');
            }
          },
          { field: 'zczj', title: '注册资金', minWidth: 80 },
          { field: 'ygrs', title: '员工人数', minWidth: 80 },
          { field: 'lxr', title: '联系人', minWidth: 80 },
          { field: 'lxdh', title: '联系电话', minWidth: 80 },
          { field: 'gsdz', title: '公司地址', minWidth: 100, showOverflow: true },
          { field: 'shdz', title: '收货地址', minWidth: 100, showOverflow: true },
          { field: 'khly', title: '客户来源', minWidth: 100, showOverflow: true },
          { field: 'khzt', title: '状态', minWidth: 100, showOverflow: true },
          { field: 'bz', title: '备注', minWidth: 100, showOverflow: true },
          { field: 'createtime', title: '创建时间', minWidth: 150, showOverflow: true },
          { field: 'createname', title: '创建人', minWidth: 100, showOverflow: true },
          { field: 'updatetime', title: '更新时间', minWidth: 150, showOverflow: true },
          { field: 'updatename', title: '更新人', minWidth: 100, showOverflow: true },
          { field: 'operation', title: '操作', fixed: 'right', width: 60, slots: { default: 'operation' } },
        ],
        data: []
      },
      PagerConfig: {
        align: 'center',
        pageSize: 25,
        total: 0,
        currentPage: 1,
        pageSizes: [10, 25, 50, 100, 200],
      },
      ChildGridOptions: {
        id:'CustomerSystemsChildGrid',
        border: true,
        stripe: true,
        loading: false,
        size: PageConfig.ComponentSize,
        height: 300,
        columnConfig: {
          resizable: true
        },
        rowConfig: {
          isHover: true,
          isCurrent:true
        },
        editConfig: {
          trigger: 'click',
          mode: 'row',
          showStatus: true
        },
        exportConfig: {
        },
        toolbarConfig: {
          tools: [
            { code: 'childImport', icon: 'vxe-icon-file-excel', circle: true },
            { code: 'childAdd', icon: 'vxe-icon-add', circle: true }
          ]
        },
        customConfig:{
          mode:'drawer',
          storage:true
        },
        columns: [
          { type: 'seq', width: 50 },
          { field: 'txid', title: '体系名称', minWidth: 150, showOverflow: true, editRender: gltxItemRender },
          { field: 'txgg', title: '体系规格', minWidth: 120, showOverflow: true },
          { field: 'rzbz', title: '认证标准', minWidth: 120, showOverflow: true },
          { field: 'zslb', title: '证书类别', minWidth: 120, showOverflow: true },
          {
            field: 'zsrq', title: '证书日期', minWidth: 120, showOverflow: true,
            editRender: { name: 'VxeDatePicker', props: { type: 'date' } }
          },
          {
            field: 'zsyxq', title: '证书有效期', minWidth: 120, showOverflow: true,
            editRender: { name: 'VxeDatePicker', props: { type: 'date' } }
          },
          {
            field: 'zszt', title: '证书状态', minWidth: 120, showOverflow: true,slots:{default:'zsztRender'}
          },
          { field: 'zsjg', title: '证书机构', minWidth: 120, showOverflow: true, editRender: { name: 'VxeInput' } },
          {
            field: 'mcshrq', title: '初审末次时间', minWidth: 150, showOverflow: true,
            editRender: { name: 'VxeDatePicker', props: { type: 'date' } }
          },
          { field: 'xcshrq', title: '下次审核提醒', minWidth: 150, showOverflow: true,editRender: { name: 'VxeDatePicker', props: { type: 'date' } }},
          { field: 'operation', title: '操作', fixed: 'right', width: 120, slots: { default: 'operation' } },
        ],
        data: []
      },
      GltxOptions: []
    }
  },
  created() {
    // 动态头部按钮
    this.btnPromission = getBtnCtr(
      this.$route.path,
      this.$store.getters.userPermission
    )
    this.GetGltxOptions();
  },
  methods: {
    ValidatorNumber(e) {
      if (!isNumber(e.itemValue)) {
        return new Error('请输入数字');
      } else {
        if (e.itemValue < 0) {
          return new Error('请输入大于等于0的数字');
        }
      }
    },
    handleQuery(e) {
      this.QueryConditions = e;
      this.DoQuery(e);
    },
    handlePageChange() {
      this.DoQuery(this.QueryConditions);
    },
    handleChildCancel() {
      this.$refs.childgrid.clearEdit();
    },
    hasEditStatus(row) {
      return this.$refs.childgrid.isEditByRow(row);
    },
    handleChildSave(row) {
      this.$refs.childgrid.clearEdit();
      row.id == 0 ? this.DoChildAdd(row) : this.DoChildEdit(row);
    },
    handleChildToolClick({ code }) {
      if (!this.editRow) {
        VxeUI.modal.message({ content: '请选择客户信息', status: 'error', duration: this.duration })
        return;
      }
      if (code == 'childAdd') {
        this.ChildGridOptions.data.push({
          id: 0,
          khid: this.editRow.id,
          txid: '',
          txgg: '',
          rzbz: '',
          zsrq: '',
          zsyxq: '',
          zsjg: '',
          mcshrq: '',
          xcshrq: '',
        })
      } else if (code == 'childImport') {

      }
    },
    handleView(row) {
      this.editRow = row;
      let queryConditions = [{
        column: 'khid',
        content: row.id,
        condition: '0'
      }]
      this.DoChildQuery(queryConditions);
    },
    handleChildEdit(row) {
      this.$refs.childgrid.setEditRow(row);
    },
    handleChildDelete(row) {
      this.DoChildDelete(row);
    },
    GetGltxOptions(e) {
      let request = {
        isPaging: false,
        pageSize: 1,
        pageIndex: 1,
        queryConditions: [],
        orderByConditions: []
      }
      CURDHelper.Query(request, '/api/Xmjcgltx')
        .then(res => {
          this.GltxOptions = res.data;
          this.gltxItemRender.options = [];
          res.data.forEach(item => {
            this.gltxItemRender.options.push({
              value: item.id,
              label: item.txmc
            })
          })
        }).catch(err => {
          VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
        })
    },
    DoQuery(e) {
      let request = {
        isPaging: true,
        pageSize: this.PagerConfig.pageSize,
        pageIndex: this.PagerConfig.currentPage,
        queryConditions: e,
        orderByConditions: []
      }
      CURDHelper.Query(request, this.BaseApiUrl)
        .then(res => {
          this.GridOptions.data = res.data;
          this.PagerConfig.total = res.totalNumber;
          this.editRow = undefined;
          this.ChildGridOptions.data = []
        }).catch(err => {
          VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
        })
    },
    DoChildEdit(row) {
      this.$refs.childgrid.clearEdit();
      let postData = {
        id: row.id,
        txid: row.txid,
        zsrq: row.zsrq,
        zsyxq: row.zsyxq,
        zsjg: row.zsjg,
        mcshrq: row.mcshrq,
        xcshrq:row.xcshrq
      }
      CURDHelper.Edit(postData, null, `${this.BaseChildApiUrl}`)
        .then(res => {
          VxeUI.modal.message({ content: '编辑数据成功', status: 'success', duration: this.duration })
          let queryConditions = [{
            column: 'xmid',
            content: this.editRow.id,
            condition: '0'
          }]
          this.DoChildQuery(queryConditions);
        }).catch(err => {
          VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
        })

    },
    DoChildQuery(e) {
      let queryConditions = e || [];
      let request = {
        isPaging: false,
        pageSize: 1,
        pageIndex: 1,
        queryConditions: queryConditions,
        orderByConditions: [{
          column: 'createtime',
          condition: 'asc'
        }]
      }
      CURDHelper.Query(request, this.BaseChildApiUrl)
        .then(res => {
          this.ChildGridOptions.data = res.data;
        }).catch(err => {
          VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
        })
    },
    DoChildAdd(row) {
      this.$refs.childgrid.clearEdit();
      CURDHelper.Add(row, null, `${this.BaseChildApiUrl}`)
        .then(res => {
          VxeUI.modal.message({ content: '编辑数据成功', status: 'success', duration: this.duration })
          let queryConditions = [{
            column: 'khid',
            content: this.editRow.id,
            condition: '0'
          }]
          this.DoChildQuery(queryConditions);
        }).catch(err => {
          VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
        })

    },
    DoChildDelete(row) {
      //删除
      let deleteModel = {
        ID: row.id,
        DeleteRemark: ''
      };
      CURDHelper.DeleteWithConfirm('是否删除当前数据', deleteModel, null, this.BaseChildApiUrl)
        .then(res => {
          VxeUI.modal.message({content: '删除数据成功',status: 'success',duration: this.duration})
          let queryConditions = [{
            column: 'khid',
            content: row.id,
            condition: '0'
          }]
          this.DoChildQuery(queryConditions);
        }).catch(err => {
          VxeUI.modal.message({content: err,status: 'error',duration: this.duration})
        })
    }
  }
}
